package jblip.apps.clip.cmds;

import java.util.Enumeration;

import jblip.BlipClient;
import jblip.apps.clip.ClipPrinter;
import jblip.resources.Update;

public class Poll extends ClipCommand {

  @Override
  public int getRequiredArgsCount() {
    return 0;
  }

  @Override
  public void execute(BlipClient client, String[] args) {
    Integer since = null;
    if (ClipCommand.hasProperty("since")) {
      since = Integer.parseInt(ClipCommand.getProperty("since"));
    }
    Integer limit = null;
    if (ClipCommand.hasProperty("limit")) {
      limit = Integer.parseInt(ClipCommand.getProperty("limit"));
    }

    Integer offset = null;
    if (ClipCommand.hasProperty("offset")) {
      offset = Integer.parseInt(ClipCommand.getProperty("offset"));
    }
    
    String user = null;
    if (args.length > 0) {
      user = args[0];
    }

    Enumeration<? extends Update> updates = client.poll(user, since, limit, offset);
    if (updates != null) {
      while (updates.hasMoreElements()) {
        ClipPrinter.print(updates.nextElement());
      }
    } else {
      System.out.println("ERROR: " + client.getLastError());
    }
  }

  @Override
  public String getName() {
    return "poll";
  }

  @Override
  public String getUsageMessage() {
    return "[user]";
  }
}
